package com.sat.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sat.goods.mapper.BrandMapper;
import com.sat.goods.model.Brand;
import com.sat.goods.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {

    @Autowired
    BrandMapper brandMapper;

    @Override
    public List<Brand> search(Brand brand) {
        QueryWrapper<Brand> brandQueryWrapper = getBrandQueryWrapper(brand);
        return baseMapper.selectList(brandQueryWrapper);
    }

    @Override
    public Page searchPage(Integer page, Integer size) {
        Page<Brand> brandPage = new Page<>(page, size);
        return brandMapper.selectPage(brandPage, null);
    }

    @Override
    public Page findPage(Integer page, Integer size, Brand brand) {
        Page<Brand> brandPage = new Page<>(page, size);
        QueryWrapper<Brand> brandQueryWrapper = getBrandQueryWrapper(brand);
        return brandMapper.selectPage(brandPage,brandQueryWrapper);
    }

    private QueryWrapper<Brand> getBrandQueryWrapper(Brand brand) {
        QueryWrapper<Brand> brandQueryWrapper = new QueryWrapper<>();
        if (brand != null) {
            if (brand.getName() != null && !"".equals(brand.getName())) {
                brandQueryWrapper.like("name", brand.getName());
            }
            if (brand.getLetter() != null) {
                brandQueryWrapper.eq("letter", brand.getLetter());
            }
        }
        return brandQueryWrapper;
    }

}
